package com.smasher.component.activity

import android.annotation.SuppressLint
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.widget.Toast
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.widget.Toolbar
import androidx.core.app.ActivityCompat
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import com.amap.api.location.AMapLocationClient
import com.smasher.component.R
import com.smasher.location.eums.LocationMode
import com.smasher.location.listener.onLocationListener
import com.smasher.location.proxy.LocationHelper
import com.smasher.widget.base.BaseActivity

class LocationActivity : BaseActivity(), ActivityCompat.OnRequestPermissionsResultCallback,
    onLocationListener {
    private val proxy by lazy {
        LocationHelper(this)
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main)) { v, insets ->
            val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
            insets
        }
    }


    @SuppressLint("InflateParams")
    override fun getRootView(): View {
        return LayoutInflater.from(this).inflate(R.layout.activity_location, null)
    }

    override fun initView() {
        val toolbar = findViewById<Toolbar>(R.id.toolbar)
        setTitleCenter(toolbar)
        toolbar.setNavigationOnClickListener { finish() }

    }

    override fun initData() {
        initAMap()
        initLocation()
        proxy.checkPermission()
    }

    private fun initAMap() {
        AMapLocationClient.updatePrivacyShow(this, true, true)
        AMapLocationClient.updatePrivacyAgree(this, true)
    }

    private fun initLocation() {
        proxy.setMode(LocationMode.NATIVE)
        proxy.setLocationListener(this)
    }

    override fun onLocation(addressDescription: String?) {
        addressDescription?.let {
            Toast.makeText(this, addressDescription, Toast.LENGTH_SHORT).show()
        }
    }

    override fun onError(error: String?) {
        Toast.makeText(this, error ?: "定位异常", Toast.LENGTH_SHORT).show()
    }

    override fun onComplete() {
        Toast.makeText(this, "定位完成", Toast.LENGTH_SHORT).show()
    }
}